//
//  XMXButton.m
//  Lovetome
//
//  Created by Xiemaoxiong on 16/7/8.
//  Copyright © 2016年 上海爱途蜜网络科技有限公司. All rights reserved.
//

#import "XMXButton.h"

@interface XMXButton()

@end

@implementation XMXButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
-(instancetype)initWithTitle:(NSString *)Title
       StateNormalTitleColor:(UIColor *)NormalTitleColor
  StateHighlightedTitleColor:(UIColor *)HighlightTitleColor
                   TitleFont:(UIFont *)TitleFont
             BackGroundColor:(UIColor *)BackgroundColor
                cornerRadius:(CGFloat )cornerRadius
                 borderColor:(UIColor *)borderColor
                 borderWidth:(CGFloat )borderWidth
  contentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment
                       block:(TouchButton)block{
    
    self = [super initWithFrame:CGRectNull];
    
    if (self) {
        if (Title) {
            [self setTitle:Title forState:UIControlStateNormal];
            [self setTitle:Title forState:UIControlStateHighlighted];
        }
        
        if (NormalTitleColor) {
            [self setTitleColor:NormalTitleColor forState:UIControlStateNormal];
        }
        
        if (HighlightTitleColor) {
            [self setTitleColor:HighlightTitleColor forState:UIControlStateHighlighted];
        }
        
        
        if (TitleFont) {
            self.titleLabel.font = TitleFont;
        }
        
        if (BackgroundColor) {
            [self setBackgroundColor:BackgroundColor];
        }
        
        if (cornerRadius) {
            self.layer.cornerRadius = cornerRadius;
        }
        
        if (borderColor) {
            self.layer.borderColor = borderColor.CGColor;
        }
        
        if (borderWidth) {
            self.layer.borderWidth = borderWidth;
        }
        
        if (contentHorizontalAlignment) {
            self.contentHorizontalAlignment = contentHorizontalAlignment;
        }
        
        if (block) {
            _block = block;
            [self addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        

    }
    return self;
}

- (void)touchAction:(id)sender{
    
    //  防止按钮被狂点击
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender];
    
    [self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f];

}

- (void)todoSomething:(id)sender


{
      _block(self);
}


@end
